package kr.or.javacafe.news.controller;

import java.io.File;
import java.util.List;

import kr.or.javacafe.demo.domain.mongo.TwitterWord;
import kr.or.javacafe.news.domain.GoogleNews;
import kr.or.javacafe.news.domain.NewsWordCount;
import kr.or.javacafe.news.service.NewsService;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/app/news")
public class NewsController {
	
	private static final Logger logger = LoggerFactory.getLogger(NewsController.class);
		
	@Autowired
	NewsService newsService;
	


	@RequestMapping(value = "/rssSync", produces = "application/json")
	@ResponseBody
	public ResponseEntity<String> rssSync() {

		List<GoogleNews> objList = newsService.getGoogleNewsUsingRSS();
		newsService.saveGoogleNews(objList);
	
		// Response
		HttpHeaders header = new HttpHeaders();
		header.add("Content-Type", "application/json; charset=UTF-8");

		String strJSON = "{\"result\" : \"OK\"}";
		
		return new ResponseEntity<String>(strJSON, header, HttpStatus.OK);
	}
	
	
	@RequestMapping(value = "/wordCountSync", produces = "application/json")
	@ResponseBody
	public ResponseEntity<String> wordCountSync() {

		//String path = "C:\\temp_test\\output";
		String path = "/home/hrkim/hadoop2/app_news/output";
		
		File dir = new File(path);
		File[] tempFileList = dir.listFiles();
		for (File temp : tempFileList) {
			if (temp.isDirectory()) {
				
				// 디렉토리를 돌아가면서 처리한다.
				File[] fileList = temp.listFiles();
				for (File f : fileList) {
					if (f.isFile()) {
						if ("part-r-00000".equals(f.getName())) {
							System.out.println(f.getName());
							
							// 인서트 시작
							newsService.getWordCountUsingHadoop(f.getPath());
							
							// 처리 완료시 파일 삭제
							f.delete();
						}
					}
				}
			}
		}

		// Response
		HttpHeaders header = new HttpHeaders();
		header.add("Content-Type", "application/json; charset=UTF-8");
		
		String strJSON = "{\"result\" : \"OK\"}";
		
		return new ResponseEntity<String>(strJSON, header, HttpStatus.OK);
	}
	
	
	
	
	
	
	
	
	@RequestMapping(value = "/searchNews", method = RequestMethod.GET)
	public String searchNews(@RequestParam(value="word",required=false) String word, Model model) {
		
		Long totalCount = newsService.findGoogleNewsCount();
		model.addAttribute("_totalCount", totalCount);

		if (StringUtils.isNotEmpty(word)) {
			List<GoogleNews> objList = newsService.findGoogleNews(word);
			model.addAttribute("_objList", objList);			
		}
		else {
			List<GoogleNews> objList = newsService.findAllGoogleNews();
			model.addAttribute("_objList", objList);						
		}
		
		model.addAttribute("_searchWord", word);
		
		return "news/searchNews";
	}

	@RequestMapping(value = "/searchNewsWordCount", method = RequestMethod.GET)
	public String searchNewsWordCount(@RequestParam(value="word",required=false) String word, Model model) {
		
		Long totalCount = newsService.getNewsWordTotalCount();
		model.addAttribute("_totalCount", totalCount);

		List<NewsWordCount> objBestWordList = newsService.findBestNewsWord();
		model.addAttribute("_bestWordList", objBestWordList);			

		if (StringUtils.isNotEmpty(word)) {
			List<NewsWordCount> objList = newsService.findNewsWord(word);
			model.addAttribute("_objList", objList);			
		}
		else {
			List<NewsWordCount> objList = newsService.findNewsWord();
			model.addAttribute("_objList", objList);						
		}
				
		return "news/searchNewsWordCount";
	}	

	
	
}














